home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
ddj0190.arc
/
STOUT.LST
< prev
next >
Wrap
File List
|
1989-12-19
|
10KB
|
330 lines
_S-CODER FOR DATA ENCRYPTION_
by Robert Stout
[LISTIN╟ ONE]
/****************************************************************/
/* Simple S-CODER file encryptor/decryptor */
/****************************************************************/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define BUF_SIZ 32768
extern char *cryptext;
extern int crypt_length;
void crypt(char *);
main(int argc, char *argv[])
{
unsigned i, n;
char *buf, *p;
FILE *infile, *outfile;
if (4 > argc)
{
puts("\aUsage: CRYPT input_file output_file key");
abort();
}
assert(buf = (char *)malloc(BUF_SIZ));
assert(infile = fopen(argv[1], "rb"));
assert(outfile = fopen(argv[2], "wb"));
cryptext = argv[3];
crypt_length = strlen(cryptext);
while (n = fread(buf, 1, BUF_SIZ, infile))
{
p = buf;
for (i = 0; i < n; ++i)
crypt(p++);
fwrite(buf, 1, n, outfile);
}
fclose(infile);
fclose(outfile);è exit(0);
}
[LISTIN╟ TWO]
/****************************************************************/
/* Simple S-CODER stream encryptor/decryptor */
/****************************************************************/
#include <stdio.h>
#include <string.h>
#include <assert.h>
extern char *cryptext;
extern int crypt_length;
void crypt(char *);
main(int argc, char *argv[])
{
char cch;
int ich;
FILE *infile;
void setraw(void);
if (2 > argc)
{
puts("\aUsage: SCRYPT key");
puts("encrypts stdin to stdout");
abort();
}
cryptext = argv[1];
crypt_length = strlen(cryptext);
setraw(); /* NOTE: setraw() will be compiler-dependent. It is used
to set stdin and stdout to raw binary mode. This is
necessary to avoid CR/LF translation and to avoid
sensing 0x1a as EOF during decryption. */
while (EOF != (ich = getchar()))
{
cch = (char)ich;è crypt(&cch);
fputc(cch, stdout);
}
exit(0);
}
[LISTIN╟ THREE]
/****************************************************************/
/* Zortech C routine to set stin and stdout to binary mode */
/****************************************************************/
#include <stdio.h>
extern FILE _iob[_NFILE];
void setraw(void)
{
_iob[0]._flag &= ~_IOTRAN;
_iob[1]._flag &= ~_IOTRAN;
}
[LISTIN╟ FOUR]
/****************************************************************/
/* Enhanced security S-CODER file encryptor/decryptor */
/****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MIN_KEYL 6
extern char *cryptext;
extern int crypt_length;
void crypt(char *);
int cryptqual(void);
long fsize;
union { /* Transposition cipher buffer */
char in[16384];
char out[128][128];
} buf;
FILE *infile, *outfile;
main(int argc, char *argv[])
{
void encrypt(void);
void decrypt(void);
if (5 > argc || NULL == strchr("EeDd", argv[1][0]))
{
puts("\aUsage: HI-CRYPT { E | D } input_file output_file key");
puts("where: E = Encrypt");
puts(" D = Decrypt");
abort();
}
assert(infile = fopen(argv[2], "rb"));
assert(outfile = fopen(argv[3], "wb"));
cryptext = argv[4];
crypt_length = strlen(cryptext);
if (cryptqual())
{è puts("\aHI-CRYPT: Key is not sufficiently complex");
abort();
}
if (strchr("Ee", argv[1][0]))
encrypt();
else decrypt();
fclose(infile);
fclose(outfile);
exit(0);
}
int cryptqual(void)
{
int i, j = 0;
static char found[MIN_KEYL + 1]; /* Statics initialized to zeros */
if (6 > crypt_length)
return -1;
for (i = 0; i < crypt_length; ++i)
{
if (strchr(found, cryptext[i]))
continue;
found[j++] = cryptext[i];
if ((MIN_KEYL - 1) < j)
return 0;
}
return -1;
}
void encrypt(void)
{
unsigned i, j, n;
fseek(infile, 0L, SEEK_END);
fsize = ftell(infile); /* Save size */
rewind(infile);
fwrite(&fsize, sizeof(long), 1, outfile);
srand((unsigned)fsize);
crypt_ptr = fsize % crypt_length;
while (n = fread(buf.in, 1, 16384, infile))
{
while (16384 > n)
buf.in[n++] = rand();
for (i = 0; i < 128; ++i)
for (j = 0; j < 128; ++j)
crypt(&buf.out[j][i]);
fwrite(buf.in, 1, 16384, outfile);
}
}
void decrypt(void)
{
unsigned i, j, n;
fread(&fsize, sizeof(long), 1, infile);è crypt_ptr = fsize % crypt_length;
while (n = fread(buf.in, 1, 16384, infile)) /* Read size */
{
for (i = 0; i < 128; ++i)
for (j = 0; j < 128; ++j)
crypt(&buf.out[j][i]);
if (16384 <= fsize)
fwrite(buf.in, 1, 16384, outfile);
else fwrite(buf.in, 1, fsize, outfile);
fsize -= n;
}
}
[LISTIN╟ FIVE]
/****************************************************************/
/* Collect file statistics */
/****************************************************************/
#include <stdio.h>
#include <math.h>
#include <assert.h>
main(int argc, char *argv[])
{
int i, ch, hist = 0;
long n = 0L;
double mean = 0., stdev = 0., ftmp;
static unsigned bins[256];
FILE *infile;
assert(infile = fopen(argv[1], "rb"));
while (!feof(infile))
{
if (EOF == (ch = fgetc(infile)))
break;
bins[ch] += 1;
++n;
}
fclose(infile);
for (i = 0; i < 256; ++i)
{
mean += (double)(bins[i]);
if (bins[i])
++hist;
}
mean /= 256.;
for (i = 0; i < 256; ++i)
{
ftmp = (double)(bins[i]) - mean;
stdev += (ftmp * ftmp);
}
ftmp = stdev / 255.;
stdev = sqrt(ftmp);
printf("%ld Characters were read from %s\n"è "There are an average of %f occurances of each character\n"
"%d Characters out of 256 possible were used\n"
"The standard deviation is %f\n"
"The coefficient of variation is %f%%\n",
n, argv[1], mean, hist, stdev, (100. * stdev) / mean);
}
Figure 1: A polyalphabetic substitution cipher
/****************************************************************/
/¬ Simple encrypt/decrypt function using exclusive-ORing */
/¬ NOTE║ Thi≤ i≤ includeΣ fo≥ demonstratioε onlyí Datß encrypted¬/
/¬ with this code will be subject to simple cryptanalysis. */
/****************************************************************/
char *cryptext; /* The encryption/decryption key */
void crypt(char *buf)
{
int crypt_ptr = 0; /* Circular pointer to elements of key */
*buf ^= cryptext[crypt_ptr];
if (++crypt_ptr >= strlen(cryptext))
crypt_ptr = 0;
}
Figure 2: The S-CODER algorithm
/****************************************************************/
/* S-CODER - Encrypt/decrypt data */
/* Copyright 1987-1989 by Robert B. Stout dba MicroFirm */
/* Originally written by Bob Stout with modifications */
/* suggested by Mike Smedley. */
/* This code may be used freely in any program for any */
/* application, personal or commercial. */
/* Current commercial availability: */
/* 1. MicroFirm Toolkit ver 3.00: LYNX and CRYPT utilities */
/* 2. CXL libraries (MSC, TC, ZTC/C++, PC): fcrypt() */
/* dedicated file encryption function */
/* 3. SMTC & SMZT libraries: crypt() function */
/****************************************************************/
char *cryptext; /* The actual encryption/decryption key */
int crypt_ptr = 0; /* Circular pointer to elements of key */
int crypt_length; /* Set externally to strlen(cryptext) */
/* NOTES: cryptext should be set and qualified (to something over
5-6 chars, minimum) by the calling program, which should
also set crypt_ptr in the range of 0 to strlen(cryptext)
before each use. If crypt() is used to encrypt several
buffers, cryptext should be reloaded and crypt_ptr reset
before each buffer is encrypted. The encryption is both
reversible - to decrypt data, pass it back through crypt()
using the original key and original initial value of
crypt_ptr - and multiple passes are commutative« */
/**** Encrypt/decrypt buffer datum ******************************/
void crypt(char *buf)
{
*buf ^= cryptext[crypt_ptr] ^ (cryptext[0] * crypt_ptr);
cryptext[crypt_ptr] += ((crypt_ptr < (crypt_length - 1)) ?
cryptext[crypt_ptr + 1] : cryptext[0]);
if (!cryptext[crypt_ptr])
cryptext[crypt_ptr] += 1;
if (++crypt_ptr >= crypt_length)
crypt_ptr = 0;
}